<html lang="en" xmlns:v-bind="http://www.w3.org/1999/xhtml">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <link rel="stylesheet" href="../bootstrap/css/bootstrap.min.css">
  <link rel="stylesheet" href="../css/userInfo.css">

  <script src="../bootstrap/js/jquery.min.js"></script>
  <script src="../bootstrap/js/bootstrap.min.js"></script>
  <script src="../js/vue.js"></script>
  <script src="../js/axios.min.js"></script>
   <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
  <script src="../js/element-ui.js"></script>
</head>
<title>权限管理</title>
<body style="background-color: #f4f9fa">
<div id="ulInfo">
  <!-- 导航 -->
  <nav class="navbar navbar-expand-md bg-white navbar-light fixed-top" style="font-size: 16px;">
    <h1 class="title">
      <a class="navbar-brand" href="#">
        <img src="../img/studyLogo.png" alt="logo" width="40" height="40">
      </a>
    </h1>
    <!-- 折叠按钮 -->
    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbar">
      <span class="navbar-toggler-icon"></span>
    </button>
    <div class="collapse navbar-collapse" id="navbar">
      <ul class="navbar-nav mr-auto">
        <li class="nav-item"><a class="nav-link" href="../main.html">首页</a></li>
        <!--<li class="nav-item"><a class="nav-link" href="collect.html">收藏</a></li>-->
        <li v-if="(currentUser.roleId == 2 || currentUser.roleId == 0)" class="nav-item"><a class="nav-link"
                                                                                            href="videoUpload.html">上传视频</a>
        </li>
        <li v-if="(currentUser.roleId == 2 || currentUser.roleId == 0)" class="nav-item"><a class="nav-link"
                                                                                            href="videoManager.html">视频管理</a>
        </li>
        <li v-if="currentUser.roleId == 0" class="nav-item"><a class="nav-link" href="userManager.html">用户管理</a>
        </li>
        <li v-if="currentUser.roleId == 0" class="nav-item"><a class="nav-link" href="functionManager.html">权限管理</a></li>
        <!--下拉-->
        <li class="dropdown" style="margin-right: 450px">
          <a class="dropdown-toggle nav-link" data-toggle="dropdown" role="button" aria-haspopup="true"
             aria-expanded="false" style="cursor:pointer;">我的 <span class="caret"></span></a>
          <ul class="dropdown-menu" style="text-align: center;">
            <li><a style="color: #6c757d;" href="userInfo.html">个人中心</a></li>
            <li><a style="color: #6c757d;" href="collect.html">我的收藏</a></li>
            <li><a style="color: #6c757d;" href="browse.html">浏览记录</a></li>
            <li><a style="color: #6c757d;" href="../userExit">退出登录</a></li>
          </ul>
        </li>

        <template v-if="currentUser.account == null">
          <li class="nav-item"><a class="nav-link" href="../login.html">你好,请登录</a></li>
          <li class="nav-item"><a class="nav-link" href="#">|</a></li>
          <li class="nav-item"><a class="nav-link" href="../register.html">免费注册</a></li>
        </template>
        <template v-if="currentUser.account != null">
          <li class="nav-item"><a class="nav-link" href="userInfo.html">欢迎你：{{currentUser.account}}</a></li>
          <li class="nav-item"><a class="nav-link" href="#">|</a></li>
          <li class="nav-item"><a class="nav-link" href="../userExit">退出</a></li>
        </template>
      </ul>
      <form id="formSearch" action="" class="form-inline ml-auto">
        <input id="searchInput" type="text" v-model="keyword" class="form-control mr-sm-2" placeholder="Search"
               @change="searchVideo">
        <input @click="searchVideo" type="button" class="btn btn-outline-secondary  my-2 my-sm-0" value="搜索">
      </form>
    </div>
  </nav>
  <div class="all" style="margin-top: 100px">

    <h2 style="text-align: center;margin-top: 30px;margin-bottom: 30px">权限管理</h2>
    <form action="" id="searchForm" name="searchForm" method="post"
          style="width: 80%;margin-left: 30%">
      <label>
        <input v-model="roleName" type="text" name="roleName" id="roleName" placeholder="角色名称"
               value="">
      </label>
      <label>
        <input v-model="roleId" type="text" name="roleId" id="roleId" placeholder="角色编码" value="">
      </label>
      <input type="button" @Click="getAllRole" value="查询">
      <input type="reset" @Click="() => {this.roleId = '';this.roleName = '';}" value="重置">
    </form>
    <span style="margin-left: 60px;color: #a8b5b5;">提示: 使用功能授权编辑角色权限</span>
    <table class="table" id="table" style="margin: 10px auto;width: 90%;font-size: 18px; font-family: '微软雅黑';">
      <tr>
        <th>角色名称</th>
        <th>角色编码</th>
        <th>操作</th>
      </tr>
      <template v-for="user in roleList.data">
        <tr :userId="user.roleId" class="hover">
          <td ondblclick="edit(this)" editType="roleName" style="width: 35%;cursor:pointer;" name="editInfo">
            {{user.roleName}}
          </td>
          <td style="width: 35%">{{user.roleId}}</td>
          <td style="width: 30%">
            <el-link type="primary" @click="functionManager(user.roleId)">功能授权</el-link>
          </td>
        </tr>
      </template>
    </table>
    <div style="width:60%;margin-left:20%; margin-bottom: 50px;text-align:center;" class="centerdiv">
      <input type="button" value="首  页" class="btnPage" @Click="pageOper('min')" style="margin-left: 20%"/>
      <input type="button" value="上一页" class="btnPage" @Click="pageOper('up')"/>
      第<input type="text" v-model="pageNo" class="btn"
              style="width: 34px;text-align: center;margin-bottom: 4px" @blur="pageOper">页&nbsp;&nbsp;<b>·</b>&nbsp;&nbsp;共
      &nbsp;<span v-text="maxPage"></span>&nbsp; 页
      <input type="button" value="下一页" class="btnPage" @Click="pageOper('down')"/>
      <input type="button" value="尾  页" class="btnPage" @Click="pageOper('max')"/>
    </div>
  </div>

  <el-dialog title="当前角色权限列表" :visible.sync="functionListVisible" width="60%">
    <el-button type="primary" style="margin: 10px;" @click="addVideoTypeManager">新增权限</el-button>
    <el-table :data="functionList" border max-height="300">
      <el-table-column property="roleName" label="角色名称" width="120"></el-table-column>
      <el-table-column property="funName" label="功能名称"></el-table-column>
      <el-table-column property="funUrl" label="功能编码"></el-table-column>
      <el-table-column fixed="right" label="操作">
        <template slot-scope="scope">
          <el-button type="danger" @click.native.prevent="deleteRoleFunction(scope.$index, functionList)"
                     size="mini">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-dialog>
</div>
</body>
<script src="../js/diqu2.js"></script>
<script type="text/javascript">
  let message = null;
  /*用户信息*/
  new Vue({
    el: "#ulInfo",
    data() {
      return {
        currentUser: "",
        roleList: "",
        roleName: "",
        roleId: "",
        sex: "",
        pageNo: "0",
        maxPage: "0",
        keyword: "",
        functionListVisible: false,
        functionList: [],
        allFunctionList: [],
      }
    },
    methods: {
      deleteRoleFunction(index, tableData) {
        let currentRow = tableData[index];
        const deleteRoleFunctionLoading = this.$loading({lock: true, text: 'Loading...', spinner: 'el-icon-loading', background: 'rgba(0, 0, 0, 0.7)'});
        axios.get('../deleteRoleFunction?roleId='+currentRow.roleId+"&funId="+currentRow.funId).then(result => {
          deleteRoleFunctionLoading.close();
          this.getFunctionList(currentRow.roleId)
          this.functionList = result.data;
        }).catch(function (error) {
          deleteRoleFunctionLoading.close();
          message.error(error.response.data)
          if (error.response.status === 401) window.location.href = "../login.html";
        });
      },
      functionManager: function (roleId) {
        this.functionListVisible = true;
        this.getFunctionList(roleId)
      },
      addFunctionManager: function () {
        this.addVideoTypeVisible = true;
      },

      getFunctionList: function (roleId) {
        const getFunctionListLoading = this.$loading({lock: true, text: 'Loading...', spinner: 'el-icon-loading', background: 'rgba(0, 0, 0, 0.7)'});
        axios.get('../queryAllFunctionByRole?roleId='+roleId).then(result => {
          getFunctionListLoading.close();
          this.functionList = result.data;
        }).catch(function (error) {
          getFunctionListLoading.close();
          message.error(error.response.data)
          if (error.response.status === 401) window.location.href = "../login.html";
        });
      },
      searchVideo: function () {
        window.location.href = "search.html?keyword=" + this.keyword;
      },
      //获取当前用户
      getCurrentUser: function () {
        const loading = this.$loading({lock: true, text: 'Loading...', spinner: 'el-icon-loading', background: 'rgba(0, 0, 0, 0.7)'});

        axios.get("../getCurrentUser")
          .then(result => {
            loading.close();
            this.currentUser = result.data;
          })
          .catch(function (error) {
            loading.close();
            message.error(error.response.data);
            if (error.response.status === 401) window.location.href = "../login.html";
          })
      },
      //获取所有用户
      getAllRole: function () {
        const loading = this.$loading({lock: true, text: 'Loading...', spinner: 'el-icon-loading', background: 'rgba(0, 0, 0, 0.7)'});

        axios.get("../getAllRole?roleName=" + this.roleName + "&roleId=" + this.roleId + "&pageNo=1")
          .then(result => {
            loading.close();
            this.roleList = result.data;
            this.pageNo = this.roleList.pageNo;
            this.maxPage = this.roleList.maxPageNo;
          })
          .catch(function (error) {
            loading.close();
            message.error(error.response.data);
            if (error.response.status === 401) window.location.href = "../login.html";
          })
      },
      //翻页操作
      pageOper: function (type) {
        let newPage = this.pageNo;
        if (type === "min") {
          newPage = 1
        } else if (type === "up") {
          newPage = newPage - 1;
          if (newPage < 1) {
            newPage = 1;
          }
        } else if (type === "down") {
          newPage = newPage + 1;
          if (newPage > this.maxPage) {
            newPage = this.maxPage;
          }
        } else if (type === "max") {
          newPage = this.maxPage;
        }
        const loading = this.$loading({lock: true, text: 'Loading...', spinner: 'el-icon-loading', background: 'rgba(0, 0, 0, 0.7)'});

        axios.get("../getAllRole?roleName=" + this.roleName + "&roleId=" + this.roleId + "&pageNo=" + newPage)
          .then(result => {
            loading.close();
            this.roleList = result.data;
            this.pageNo = this.roleList.pageNo;
            this.maxPage = this.roleList.maxPageNo;
          })
          .catch(function (error) {
            loading.close();
            message.error(error.response.data);
            if (error.response.status === 401) window.location.href = "../login.html";
          })
      },
    },
    mounted() {
      message = this.$message;
      this.getCurrentUser();
      this.getAllRole();
    },
    filters: {
      formatDate(time) {
        var date = new Date(time);
        var year = date.getFullYear();
        var month = date.getMonth() + 1;
        var date = date.getDate();
        return year + "-" + month + "-" + date
      },
      //格式化角色类型
      formatRole(roleId) {
        if (roleId === 0) {
          return "管理员";
        } else if (roleId === 1) {
          return "学生";
        } else if (roleId === 2) {
          return "老师";
        } else if (roleId === 3) {
          return "VIP学生";
        } else if (roleId === 4) {
          return "账号禁用";
        }
        return "未知"
      },
    }
  });

  //编辑信息
  function edit(td) {
    //获取旧文本
    let old = td.innerText;
    //获取修改的类型
    let type = td.getAttribute('editType');
    //获取用户的id
    userId = td.parentNode.getAttribute("userId");
    if (type === "birthday") {
      td.innerHTML = "<input type='date' size='20' style='width:100%;'>";
    } else {
      //修改其他
      td.innerHTML = "<input type='text' size='20' style='width:80%;'>";
    }
    let input = td.firstChild;

    input.value = old;
    input.select();
    //绑定输入框失去焦点事件,更新
    input.onblur = function () {
      update(this, old, type);
    }
  }

  /**
   * 失去焦点，更新
   */
  function update(input, old, type) {
    let newText = input.value;
    //input的父节点是td
    let TD = input.parentNode;

    if (!newText) {
      message.error('您的输入为空，修改失败！');
      TD.innerText = old;
      return;
    }


    axios.get("../userInfoUpdate?uid=" + userId + "&editType=" + type + "&newText=" + newText)
      .then(result => {
        TD.innerText = newText;
        if (newText != old) {
          message.success("更新成功")
        }
      }).catch(function (error) {
      TD.innerText = old;
      message.error(error.response.data);
            if (error.response.status === 401) window.location.href = "../login.html";
    })
  }
</script>
</html>
